﻿using System;

namespace DG.Common.Validation
{
    public abstract class ValidatorBase : IValidator
    {
        public Func<string> ErrorMessage { get; set; }

        public Func<bool> PreconditionMethod { get; set; }

        public abstract Func<bool> ValidationMethod();

        public bool IsValid
        {
            get { return DoValidation(); }
        }

        public bool DoValidation()
        {
            return (PreconditionMethod != null && !PreconditionMethod()) || ValidationMethod()();
        }

        protected ValidatorBase(Func<string> errorMessage, Func<bool> preCondition)
        {
            this.ErrorMessage = errorMessage ?? (this.ToString);
            this.PreconditionMethod = preCondition;
        }
    }

    
}